package com.ysxm.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.web.multipart.MultipartFile;

public class MulitPartUtil {
     public static String fileUploadUtil(MultipartFile mf, String path){
    	File file=new File(path);
 		if (!file.exists()){
 			file.mkdirs();
 		}
 		// 获取文件名
 		String oldFileName = mf.getOriginalFilename();
 		// 将路径和文件名包装到File对象生成文件对象
 		File file1 = new File(path + File.separator + oldFileName);
 		// 将当前文件数据to给谁，这里to给tomcat目录下生成图片
 		try {
 			String newName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) +"."+ oldFileName.substring(oldFileName.lastIndexOf(".")+1);
			mf.transferTo(file1);
			return newName;
		} catch (IllegalStateException  e) {
			e.printStackTrace();
			 return null;
		} catch (IOException e) {
			e.printStackTrace();
			 return null;
		}
     }
     
     public static boolean FileDelete(String path, String oldFileName) {
 		File file = new File(path+"/"+oldFileName);
 		return file.delete();
 	}
}
